﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WorkFlowSystem.DataBase;
using WorkFlowSystem.Common;
using WorkFlowSystem.Business;

namespace WorkFlowSystem
{
    public partial class HomeUi : Form
    {
        public HomeUi()
        {
            InitializeComponent();
            this.expandablePanel_WorkMngt.ExpandedChanging += new DevComponents.DotNetBar.ExpandChangeEventHandler(this.expandablePanel_WorkMngt_Click);
            this.expandablePanel_DptMngt.ExpandedChanging += new DevComponents.DotNetBar.ExpandChangeEventHandler(this.expandablePanel_DptMngt_Click);
            this.expandablePanel_HumanMngt.ExpandedChanging += new DevComponents.DotNetBar.ExpandChangeEventHandler(this.expandablePanel_HumanMngt_Click);
            this.expandablePanel_CompanyMngt.ExpandedChanging += new DevComponents.DotNetBar.ExpandChangeEventHandler(this.expandablePanel_CompanyMngt_Click);

            string n_title = "";
            string n_text = "";
            string u_name = "";
            string n_update_date = "";

            this.getNoticeMsg(ref n_title, ref n_text, ref u_name, ref n_update_date);

            news_title.Text = n_title;
            news_contents.Text = n_text;
            news_updater.Text = u_name;
            news_date.Text = n_update_date; 

        }

        #region 主界面各种控制
        
        private void HomeUi_Load(object sender, EventArgs e)
        {
            customize_ui(CurrentUser.user_auth);
            refresh_EmployeeMngt();
        }

        private void remove_tabpages()
        {
            superTabControl1.Tabs.Remove(superTabItem2);
            superTabControl1.Tabs.Remove(superTabItem4);
            superTabControl1.Tabs.Remove(superTabItem5);
            superTabControl1.Tabs.Remove(superTabItem6);
            superTabControl1.Tabs.Remove(superTabItem7);

            superTabControl1.Tabs.Remove(superTabItem9);
            superTabControl1.Tabs.Remove(superTabItem11);
            superTabControl1.Tabs.Remove(superTabItem12);
        }

        private void superTabControl1_TabStripMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (superTabControl1.SelectedTabIndex == 0) return;
            //CommonDefine.clear_values_dotnetbar(superTabControl1.Tabs[superTabControl1.SelectedTabIndex]);
            //Common.CommonDefine.clear_values_dotnetbar(superTabControl1.Tabs[superTabControl1.SelectedTabIndex]);
            superTabControl1.Tabs.RemoveAt(superTabControl1.SelectedTabIndex);
            
        }

        /// <summary>
        /// 双击删除当前tab
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void superTabControl1_DoubleClick(object sender, EventArgs e)
        {
            if (superTabControl1.SelectedTabIndex == 0) return;

            //Common.CommonDefine.clear_values(tabControl1.TabPages[tabControl1.SelectedIndex]);
            superTabControl1.Tabs.RemoveAt(superTabControl1.SelectedTabIndex);
        }
        /// <summary>
        /// 根据用户权限生成初始界面
        /// </summary>
        /// <param name="auth"></param>
        private void customize_ui(Authority auth)
        {
            switch (auth)
            {
                case Authority.USER:
                    {
                        expandablePanel_DptMngt.Visible = false;
                        expandablePanel_HumanMngt.Visible = false;
                        expandablePanel_CompanyMngt.Visible = false;
                        remove_tabpages();
                    }
                    break;
                case Authority.MANAGER:
                    {
                        expandablePanel_HumanMngt.Visible = false;
                        expandablePanel_CompanyMngt.Visible = false;
                        remove_tabpages();
                    }
                    break;
                case Authority.BOSS:
                    {
                        remove_tabpages();
                    }
                    break;
                case Authority.ADMIN:
                    {
                        remove_tabpages();
                    }
                    break;
                case Authority.UNKNOW:
                    {
                        MessageBox.Show("User authority unknown");
                        this.Close();
                    }
                    break;
                default:
                    {
                        MessageBox.Show("Authority " + auth.ToString() + " is invalid");
                        this.Close();
                    }
                    break;
            }
        }

        private void expandablePanel_WorkMngt_Click(object sender, EventArgs e)
        {

            expandablePanel_DptMngt.Expanded = false;
            expandablePanel_HumanMngt.Expanded = false;
            expandablePanel_CompanyMngt.Expanded = false;

        }

        private void expandablePanel_DptMngt_Click(object sender, EventArgs e)
        {
            expandablePanel_WorkMngt.Expanded = false;

            expandablePanel_HumanMngt.Expanded = false;
            expandablePanel_CompanyMngt.Expanded = false;
        }

        private void expandablePanel_HumanMngt_Click(object sender, EventArgs e)
        {
            expandablePanel_WorkMngt.Expanded = false;
            expandablePanel_DptMngt.Expanded = false;

            expandablePanel_CompanyMngt.Expanded = false;
        }

        private void expandablePanel_CompanyMngt_Click(object sender, EventArgs e)
        {
            expandablePanel_WorkMngt.Expanded = false;
            expandablePanel_DptMngt.Expanded = false;
            expandablePanel_HumanMngt.Expanded = false;

        }
        #endregion
                
        #region 非主流功能
        /// <summary>
        /// 用户密码设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_UserSetting_Click(object sender, EventArgs e)
        {
            UserSettingUi us = new UserSettingUi();
            us.Show();
        }

        /// <summary>
        /// 职位手册
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_PositionManaul_Click(object sender, EventArgs e)
        {
            string p_name = "";
            string p_info = "";
            getPositionMsg(ref p_name,ref p_info);


            PositionUi pu = new PositionUi(p_name, p_info);
            pu.Show();
        }

        /// <summary>
        /// 系统设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_SystemManual_Click(object sender, EventArgs e)
        {
            MessageBox.Show("暂无此功能。");
        }

        /// <summary>
        /// 计算器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_Calculator_Click(object sender, EventArgs e)
        {
            Calculator calc = new Calculator();
            calc.Show(); 
        }

        /// <summary>
        /// 日历
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_Calendar_Click(object sender, EventArgs e)
        {
            Calendar cale = new Calendar();
            cale.Show();
        }

        /// <summary>
        /// 通讯录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_AddressList_Click(object sender, EventArgs e)
        {
            MessageBox.Show("暂无此功能。");
        }

        /// <summary>
        /// 帮助
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_Help_Click(object sender, EventArgs e)
        {
            MessageBox.Show("暂无此功能。");
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_ExitSystem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("退出成功。欢迎下次使用。");
            this.Close();
        }
        #endregion

        #region tabpage控制:工作管理
        /// <summary>
        /// 常规信息记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_NormalWorkInfo_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem2)))
            {
                superTabControl1.Tabs.Add(superTabItem2);
                superTabControl1.SelectedTab = superTabItem2;

                DataTable dt = new DataTable();

                WorkInfoDv wi = new WorkInfoDv();

                int dataDnt = 0;

                //获取初期数据        
                wi.getWorkData(true, null, null, null, null, ref dt, ref dataDnt);

                //调用共通方法，第六列为button列
                viewShow(dataGridViewX1, dt,6);

                workInfoSelectLoad();

                labelX2.Text = "当前检索结果共0条记录。";

            }
            else
            {
                this.superTabControl1.SelectedTab = superTabItem2;
            }

        }

        //工作记录下拉框初始化
        private void workInfoSelectLoad()
        {   
            //获取系统年份
            string year = DateTime.Now.Year.ToString();
            //获取系统月份
            string month = DateTime.Now.Month.ToString();

            //从2014年起，显示到当前年份+2年
            for(int i = 2014; i < int.Parse(year)+3;i++)
            {
                comboBoxEx1.Items.Add(i.ToString());
            }

            //默认选中系统年份
            comboBoxEx1.SelectedItem = (Object)year;
            //只能下拉 不能手动填写
            comboBoxEx1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
                       
                        
            //12个月
            for (int i = 0; i < 12; i++)
            {
                comboBoxEx2.Items.Add((i+1).ToString());
            }
            
            //默认选中系统月份
            comboBoxEx2.SelectedItem = (Object)month;
            //只能下拉 不能手动填写
            comboBoxEx2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            //记录状态
            comboBoxEx3.Items.Add("所有状态");
            comboBoxEx3.Items.Add("未填写");
            comboBoxEx3.Items.Add("已保存");
            comboBoxEx3.Items.Add("已提交");
            comboBoxEx3.Items.Add("已否认");
            comboBoxEx3.Items.Add("已承认");
            //默认选中所有状态
            comboBoxEx3.SelectedItem = (Object)"所有状态";
            //只能下拉 不能手动填写
            comboBoxEx3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        }


        //工作记录查询功能
        private void buttonX22_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();

            WorkInfoDv wi = new WorkInfoDv();

            int dataDnt = 0;

            //获取查询结果       
            wi.getWorkData(false, CurrentUser.user_id.ToString(), comboBoxEx1.SelectedItem.ToString(), comboBoxEx2.SelectedItem.ToString(), comboBoxEx3.SelectedItem.ToString(), ref dt, ref dataDnt);

            viewShow(dataGridViewX1, dt,6);
            
            if (dataDnt == 0)
            {
                MessageBox.Show("当前检索结果共0条记录。");
            }

            labelX2.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";

        }

        //行的查看按钮功能
        private void dataGridViewX1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridViewX1.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {

                    WorkInfoInput wii = new WorkInfoInput(false, false, CurrentUser.user_id.ToString(), dataGridViewX1.CurrentRow.Cells[7].Value.ToString(), dataGridViewX1.CurrentRow.Cells[8].Value.ToString(), dataGridViewX1.CurrentRow.Cells[9].Value.ToString(), dataGridViewX1.CurrentRow.Cells[2].Value.ToString());
                    
                    if (wii.dataExistFlag == true)
                    {
                        wii.ShowDialog();
                    }
                }
            }
        }
        

        #endregion

        #region tabpage控制:工作审批
        /// <summary>
        /// 工作审批
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_DptWorkMngt_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem4)))
            {
                superTabControl1.Tabs.Add(superTabItem4);
                superTabControl1.SelectedTab = superTabItem4;

                DataTable dt = new DataTable();

                WorkReviewDv wr = new WorkReviewDv();

                int dataDnt = 0;

                //获取初期数据        
                wr.getWorkData(true, null, null, null, null, ref dt, ref dataDnt);

                //调用共通方法，第六列为button列
                viewShow(WrvDataGridViewX, dt, 7);

                workReviewSelectLoad();

                WrvLabelX4.Text = "当前检索结果共0条记录。";

            }
            else
            {
                this.superTabControl1.SelectedTab = superTabItem4;
            }
        }

        //工作审批下拉框初始化
        private void workReviewSelectLoad()
        {
            //获取系统年份
            string year = DateTime.Now.Year.ToString();
            //获取系统月份
            string month = DateTime.Now.Month.ToString();

            //从2014年起，显示到当前年份+2年
            for (int i = 2014; i < int.Parse(year) + 3; i++)
            {
                WrvComboBoxEx1.Items.Add(i.ToString());
            }

            //默认选中系统年份
            WrvComboBoxEx1.SelectedItem = (Object)year;
            //只能下拉 不能手动填写
            WrvComboBoxEx1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;


            //12个月
            for (int i = 0; i < 12; i++)
            {
                WrvComboBoxEx2.Items.Add((i + 1).ToString());
            }

            //默认选中系统月份
            WrvComboBoxEx2.SelectedItem = (Object)month;
            //只能下拉 不能手动填写
            WrvComboBoxEx2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

            //记录状态
            WrvComboBoxEx3.Items.Add("已提交");
            WrvComboBoxEx3.Items.Add("已否认");
            WrvComboBoxEx3.Items.Add("已承认");
            //默认选中所有状态
            WrvComboBoxEx3.SelectedItem = (Object)"已提交";
            //只能下拉 不能手动填写
            WrvComboBoxEx3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        }


        //工作审批记录查询
        private void WrvBbuttonX1_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();

            WorkReviewDv wr = new WorkReviewDv();

            int dataDnt = 0;

            //获取查询结果 1 非初始化，2 用户ID ，3 年，4 月，5 日       
            wr.getWorkData(false, CurrentUser.user_id.ToString(), WrvComboBoxEx1.SelectedItem.ToString(), WrvComboBoxEx2.SelectedItem.ToString(), WrvComboBoxEx3.SelectedItem.ToString(), ref dt, ref dataDnt);

            viewShow(WrvDataGridViewX, dt, 7);

            if (dataDnt == 0)
            {
                MessageBox.Show("当前检索结果共0条记录。");
            }

            WrvLabelX4.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";
        }

        //行的查看按钮功能
        private void WrvDataGridViewX_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = WrvDataGridViewX.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {
                    // bool isBossFlag, string uid, string sid, string w_start_date, string w_end_date,string w_status
                    WorkInfoInput wii = new WorkInfoInput(true, false, WrvDataGridViewX.CurrentRow.Cells[7].Value.ToString(), WrvDataGridViewX.CurrentRow.Cells[8].Value.ToString(), WrvDataGridViewX.CurrentRow.Cells[9].Value.ToString(), WrvDataGridViewX.CurrentRow.Cells[10].Value.ToString(), WrvDataGridViewX.CurrentRow.Cells[3].Value.ToString());

                    if (wii.dataExistFlag == true)
                    {
                        wii.ShowDialog();
                    }
                }
            }
        }


        #endregion

        #region tabpage控制:部门管理-其他
        /// <summary>
        /// 业务指标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_Target_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem5)))
            {
                superTabControl1.Tabs.Add(superTabItem5);
                superTabControl1.SelectedTab = superTabItem5;

                DataTable dt = new DataTable();
                BIZStandard si = new BIZStandard();
                int dataDnt = 0;
                si.getStandardData(true, ref dt, ref dataDnt);
                viewShow(dataGridViewX_Standard, dt, 5);
                labelX7.Text = "当前检索结果共0条记录。";
            }
            else
                superTabControl1.SelectedTab = superTabItem5;
        }

        /// <summary>
        /// 标准关联
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_StandardMngt_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem6)))
            {
                superTabControl1.Tabs.Add(superTabItem6);
                superTabControl1.SelectedTab = superTabItem6;

                DataTable dt = new DataTable();
                BIZUserStandard us = new BIZUserStandard();
                int dataDnt = 0;
                us.getUserStandardData(true, ref dt, ref dataDnt);
                viewShow(dataGridViewX_UserStandard, dt, 9);
                labelX11.Text = "当前检索结果共0条记录。";
            }
            else
                superTabControl1.SelectedTab = superTabItem6; 
        }
        #endregion

        #region tabpage控制:人事管理
        /// <summary>
        /// 员工信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_EmployeeMngt_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem7)))
            {
                superTabControl1.Tabs.Add(superTabItem7);
                superTabControl1.SelectedTab = superTabItem7;

                DataTable dt = new DataTable();
                BIZUserInfo ui = new BIZUserInfo();
                int dataDnt = 0;
                ui.getUserInfoData(true, comboBoxEx1_EmployeeMngt.SelectedItem.ToString(), ref dt, ref dataDnt);
                viewShow(dataGridViewX2_EmployeeMngt, dt, 7);
                labelX12.Text = "当前检索结果共0条记录。";
            }
            else
                superTabControl1.SelectedTab = superTabItem7;
        }
        #endregion

        #region tabpage控制:公司管理
        /// <summary>
        /// 组织部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_DptInfo_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem9)))
            {
                superTabControl1.Tabs.Add(superTabItem9);
                superTabControl1.SelectedTab = superTabItem9;

                DataTable dt = new DataTable();
                BIZDptInfo di = new BIZDptInfo();
                
                int dataDnt = 0;

                //获取初期数据        
                di.getDptData(true, ref dt, ref dataDnt);

                viewShow(DataGridViewX1_DptInfo, dt, 6);
                labelX13.Text = "当前检索结果共0条记录。";
            }
            else
                superTabControl1.SelectedTab = superTabItem9;
        }

        /// <summary>
        /// 职位管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_PositionMngt_Click(object sender, EventArgs e)
        {
            if (!superTabControl1.Tabs.Contains((superTabItem12)))
            {
                superTabControl1.Tabs.Add(superTabItem12);
                superTabControl1.SelectedTab = superTabItem12;

                DataTable dt = new DataTable();
                BIZPositionInfo pi = new BIZPositionInfo();
                int dataDnt = 0;
                //获取初期数据        
                pi.getPositionData(true, comboBoxEx1_PositionMngt.SelectedItem.ToString(), ref dt, ref dataDnt);
                viewShow(dataGridViewX2_PositionMngt, dt, 4);
                labelX14.Text = "当前检索结果共0条记录。";
            }
            else
                superTabControl1.SelectedTab = superTabItem12;
        }

        /// <summary>
        ///通知管理 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_NoticeMngt_Click(object sender, EventArgs e)
        {
            NewsEdit ne = new NewsEdit();
        }

        /// <summary>
        /// 数据分析
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX_DataAnalysis_Click(object sender, EventArgs e)
        {
            MessageBox.Show("暂无此功能。");
            //if (!superTabControl1.Tabs.Contains((superTabItem11)))
            //{
            //    superTabControl1.Tabs.Add(superTabItem11);
            //    superTabControl1.SelectedTab = superTabItem11;
            //}
        }
        #endregion

        #region 业务指标
        /// <summary>
        /// 标准更改后自动刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void standard_OnChange(object sender, EventArgs e)
        {
            buttonX1_Standard_Click(sender, e);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Standard_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            BIZStandard si = new BIZStandard();
            int dataDnt = 0;
            si.getStandardData(false, ref dt, ref dataDnt);
            viewShow(dataGridViewX_Standard, dt, 5);
            labelX7.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Standard_Click(object sender, EventArgs e)
        {
            StandardUi sui = new StandardUi();
            sui.OnChange += new StandardUi.StandardChange(standard_OnChange);
            sui.SetMode(false, 0);
            sui.Show(); 
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewX_Standard_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridViewX_Standard.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {
                    int sid = 0;
                    try
                    {
                        sid = int.Parse(dataGridViewX_Standard.CurrentRow.Cells["sid"].Value.ToString());
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLog(ex.Message);
                        MessageBox.Show("非法的标准ID");
                        return;
                    }

                    StandardUi sui = new StandardUi();
                    sui.OnChange += new StandardUi.StandardChange(standard_OnChange);
                    sui.SetMode(true, sid);
                    sui.Show();
                }
            }
        }

        #endregion

        #region 标准关联
        /// <summary>
        /// 标准关联更改后自动刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void userStandard_OnChange(object sender, EventArgs e)
        {
            buttonX1_UserStandard_Click(sender, e);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_UserStandard_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            BIZUserStandard us = new BIZUserStandard();
            int dataDnt = 0;
            us.getUserStandardData(false, ref dt, ref dataDnt);
            viewShow(dataGridViewX_UserStandard, dt, 9);
            labelX11.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_UserStandard_Click(object sender, EventArgs e)
        {
            UserStandardUi usi = new UserStandardUi();
            usi.OnChange += new UserStandardUi.UserStandardChange(userStandard_OnChange);
            usi.SetMode(false, "", "", "", "");
            usi.Show();
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewX_UserStandard_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridViewX_UserStandard.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {
                    string us_uid = dataGridViewX_UserStandard.CurrentRow.Cells["us_uid"].Value.ToString();
                    string us_sid = dataGridViewX_UserStandard.CurrentRow.Cells["us_sid"].Value.ToString();
                    string us_start_date = dataGridViewX_UserStandard.CurrentRow.Cells["us_start_date"].Value.ToString();
                    string us_end_date = dataGridViewX_UserStandard.CurrentRow.Cells["us_end_date"].Value.ToString();

                    UserStandardUi usi = new UserStandardUi();
                    usi.OnChange += new UserStandardUi.UserStandardChange(userStandard_OnChange);
                    usi.SetMode(true, us_uid, us_sid, us_start_date, us_end_date);
                    usi.Show();
                }
            }
        }
        #endregion

        #region 员工信息
        /// <summary>
        /// user_info更改后自动刷新datagridview.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ui_OnChange(object sender, EventArgs e)
        {
            buttonX1_EmployeeMngt_Click(sender, e);
            buttonX1_DptInfo_Click(sender, e);
        }

        private void refresh_EmployeeMngt()
        {
            Logger.WriteLog("init_userinfo");
            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
                return;
            string sql = "select d_name from department";
            DataTable dt = new DataTable();
            if (db_op.ExecuteDataTable(sql, null, false, ref dt) < 0)
                return;
            db_op.CloseDB();
            comboBoxEx1_EmployeeMngt.Items.Clear();
            comboBoxEx1_EmployeeMngt.Items.Add("所有部门");
            comboBoxEx1_PositionMngt.Items.Clear();
            comboBoxEx1_PositionMngt.Items.Add("所有部门");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                comboBoxEx1_EmployeeMngt.Items.Add(dt.Rows[i]["d_name"].ToString());
                comboBoxEx1_PositionMngt.Items.Add(dt.Rows[i]["d_name"].ToString());
            }
            comboBoxEx1_EmployeeMngt.SelectedIndex = 0;
            comboBoxEx1_PositionMngt.SelectedIndex = 0;
        }

        /// <summary>
        /// 判断当前用户是否有权限修改该员工信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        private bool is_my_fellow(string dpt_name)
        {
            if (CurrentUser.user_auth == Authority.BOSS || CurrentUser.user_auth == Authority.ADMIN)
                return true;

            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接");
                return false;
            }
            string sql = "select d.d_name from user_info, department as d " +
                "where u_uid = @uid and u_did=d.did";
            List<SqlParameter> para_list = new List<SqlParameter>();
            para_list.Add(new SqlParameter("@uid", CurrentUser.user_id));
            DataTable dt = new DataTable();
            if (db_op.ExecuteDataTable(sql, para_list, false, ref dt) < 0)
            {
                Logger.WriteLog("cannot judge is_my_fellow");
                MessageBox.Show("当前用户不存在 或 选中用户的部门不正确");
                db_op.CloseDB();
                //we just exit the Application here, is it ok???
                this.Close();
                return false;
            }

            db_op.CloseDB();
            if (dt.Rows.Count > 0)
                return dt.Rows[0]["d_name"].ToString() == dpt_name.ToString();
            else
                return false;
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewX2_EmployeeMngt_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridViewX2_EmployeeMngt.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {
                    string uid = dataGridViewX2_EmployeeMngt.CurrentRow.Cells["uid"].Value.ToString();
                    string dpt_name = dataGridViewX2_EmployeeMngt.CurrentRow.Cells["ui_dpt_name"].Value.ToString();

                    if (!is_my_fellow(dpt_name))
                    {
                        MessageBox.Show("你没有修改该员工信息的权限");
                        return;
                    }

                    UserInfoUi ui = new UserInfoUi();
                    ui.OnChange += new UserInfoUi.UserInfoChange(ui_OnChange);
                    ui.SetMode(true, uid);
                    ui.Show();
                }
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_EmployeeMngt_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            BIZUserInfo ui = new BIZUserInfo();
            int dataDnt = 0;
            ui.getUserInfoData(false, comboBoxEx1_EmployeeMngt.SelectedItem.ToString(), ref dt, ref dataDnt); ;
            viewShow(dataGridViewX2_EmployeeMngt, dt, 7);
            labelX12.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_EmployeeMngt_Click(object sender, EventArgs e)
        {
            UserInfoUi ui = new UserInfoUi();
            ui.OnChange += new UserInfoUi.UserInfoChange(ui_OnChange);
            ui.SetMode(false, "");
            ui.Show();
        }
        #endregion

        #region 组织部门
        //department更改后自动刷新datagridview.
        private void dpt_edit_OnChange(object sender, EventArgs e)
        {
            buttonX1_DptInfo_Click(sender, e);
            refresh_EmployeeMngt();
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_DptInfo_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            BIZDptInfo di = new BIZDptInfo();
            int dataDnt = 0;
            di.getDptData(false, ref dt, ref dataDnt);
            viewShow(DataGridViewX1_DptInfo, dt, 6);
            labelX13.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_DptInfo_Click(object sender, EventArgs e)
        {
            DepartmentEditUi dpt_edit = new DepartmentEditUi();
            dpt_edit.SetMode(false, 0);
            dpt_edit.OnChange += new DepartmentEditUi.DepartmentChange(dpt_edit_OnChange);
            dpt_edit.Show();
        }

        /// <summary>
        /// 查看部门结构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX3_DptInfo_Click(object sender, EventArgs e)
        {
            DepartmentTreeUi dpt_tree = new DepartmentTreeUi();
            dpt_tree.Show();
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataGridViewX1_DptInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = DataGridViewX1_DptInfo.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {
                    int did = 0;
                    try
                    {
                        did = int.Parse(DataGridViewX1_DptInfo.CurrentRow.Cells["did"].Value.ToString());
                    }
                    catch
                    {
                        MessageBox.Show("非法的部门ID");
                        return;
                    }
                    DBOperator db_op = new DBOperator();
                    if (db_op.ConnectDB() < 0)
                    {
                        MessageBox.Show("无法连接服务器，请检查网络连接");
                        return;
                    }
                    string sql = "select * from department where did = @did";
                    List<SqlParameter> para_list = new List<SqlParameter>();
                    para_list.Add(new SqlParameter("@did", did));
                    DataTable dt = new DataTable();
                    db_op.ExecuteDataTable(sql, para_list, false, ref dt);
                    if (dt.Rows.Count == 0)
                    {
                        MessageBox.Show("部门不存在");
                        db_op.CloseDB();
                        return;
                    }
                    db_op.CloseDB();
                    DepartmentEditUi dpt_edit = new DepartmentEditUi();
                    dpt_edit.OnChange += new DepartmentEditUi.DepartmentChange(dpt_edit_OnChange);
                    dpt_edit.SetMode(true, did);
                    dpt_edit.Show();
                }
            }
        }
        #endregion

        #region 职位管理
        private void pie_OnChange(object sender, EventArgs e)
        {
            buttonX1_PositionMngt_Click(sender, e);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_PositionMngt_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            BIZPositionInfo pi = new BIZPositionInfo();
            int dataDnt = 0;
            pi.getPositionData(false, comboBoxEx1_PositionMngt.SelectedItem.ToString(), ref dt, ref dataDnt);
            viewShow(dataGridViewX2_PositionMngt, dt, 4);
            labelX14.Text = "当前检索结果共" + dataDnt.ToString() + "条记录。";
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_PositionMngt_Click(object sender, EventArgs e)
        {
            PositionInfoEdit pie = new PositionInfoEdit();
            pie.OnChange += new PositionInfoEdit.PositionChange(pie_OnChange);
            pie.SetMode(false, "");
            pie.Show();
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridViewX2_PositionMngt_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DataGridViewColumn column = dataGridViewX2_PositionMngt.Columns[e.ColumnIndex];

                if (column is DataGridViewButtonColumn)
                {
                    string pid = dataGridViewX2_PositionMngt.CurrentRow.Cells["pid"].Value.ToString();

                    PositionInfoEdit pie = new PositionInfoEdit();
                    pie.OnChange += new PositionInfoEdit.PositionChange(pie_OnChange);
                    pie.SetMode(true, pid);
                    pie.Show();
                }
            }
        }
        #endregion

        #region DATAGRIDVIEW共通函数
        //表格显示布局
        private void viewShow(DataGridView dv, DataTable dt, int buttonColumnId)
        {
            
            //将DataTable绑定到DataView中
            DataView dvListViewInfo = new DataView(dt);


            //取消按默认方式显示
            dv.AutoGenerateColumns = false;

            //绑定DataView到dataGridView上
            dv.DataSource = dvListViewInfo;

            dv.RowsDefaultCellStyle.BackColor = Color.White;
            //奇数行颜色
            dv.AlternatingRowsDefaultCellStyle.BackColor = Color.LightCyan;

            //无数据时查看BUTTON隐藏
            for (int i = 0; i < dv.Rows.Count; i++)
            {
                if (dv.Rows[i].Cells[1].Value == null)
                {
                    dv.Rows[i].Cells[buttonColumnId-1] = new DataGridViewTextBoxCell();
                }
                else
                {
                    if (dv.Rows[i].Cells[1].Value.ToString() == "")
                    {
                        dv.Rows[i].Cells[buttonColumnId-1] = new DataGridViewTextBoxCell();
                    }
                }

            }         

        }
        
        #endregion

        #region 获取通知信息

        private int getNoticeMsg(ref string n_title, ref string n_text, ref string u_name, ref string n_update_date)
        {

            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接。");
                return -1;
            }

            string sql = "select CONVERT(varchar(10), news.n_update_date, 120) as n_update_date , news.n_title,news.n_text, user_info.u_name from news,user_info where nid = '1000' and news.n_update_uid = user_info.uid ";

            DataTable dt = new DataTable();

            db_op.ExecuteDataTable(sql, null, false, ref dt);

            if (dt.Rows.Count == 0)
            {
                n_title = "";
                n_text = "";
                u_name = "";
                n_update_date = "";
            }
            else
            {
                n_title = dt.Rows[0]["n_title"].ToString();
                n_text = "  " + dt.Rows[0]["n_text"].ToString();
                u_name = dt.Rows[0]["u_name"].ToString();
                n_update_date = dt.Rows[0]["n_update_date"].ToString();
            }
            return 0;

        }

        #endregion

        #region 获取职位手册信息

        private int getPositionMsg(ref string p_title, ref string p_text)
        {

            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接。");
                return -1;
            }

            string sql = "select p.p_name,p.p_info from position_info p,user_info u where u.uid = @uid and u.u_pid = p.pid ";

            DataTable dt = new DataTable();
            List<SqlParameter> para_list = new List<SqlParameter>();
            para_list.Add(new SqlParameter("@uid", CurrentUser.user_id));

            db_op.ExecuteDataTable(sql, para_list, false, ref dt);

            if (dt.Rows.Count == 0)
            {
                p_title = "";
                p_text = "";
            }
            else
            {
                p_title = dt.Rows[0]["p_name"].ToString();
                p_text = "  " + dt.Rows[0]["p_info"].ToString();
               
            }
            return 0;

        }

        #endregion


    }
}
